package ui.clustering;

import java.awt.Dimension;
import java.util.HashMap;

import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.Vertex;
import edu.uci.ics.jung.visualization.AbstractLayout;
import edu.uci.ics.jung.visualization.Coordinates;

public class CoordinateLayout extends AbstractLayout
{
    protected HashMap<String, double[]> coordinateInfo = null;

    public CoordinateLayout(Graph g, HashMap<String, double[]> coordinateInfo)
    {
        super(g);
        this.coordinateInfo = coordinateInfo;
    }

    @Override
    public void advancePositions()
    {
    }

    @Override
    protected void initialize_local_vertex(Vertex v)
    {
    }

    @Override
    public boolean incrementsAreDone()
    {
        return false;
    }

    @Override
    public boolean isIncremental()
    {
        return false;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void initializeLocations()
    {
        super.initializeLocations();

        Vertex[] vertices = (Vertex[]) getVisibleVertices().toArray(
                new Vertex[0]);

        Dimension d = getCurrentSize();
        double height = d.getHeight();
        double width = d.getWidth();

        for (int i = 0; i < vertices.length; i++)
        {
            Coordinates coord = getCoordinates(vertices[i]);
            double[] coordinate = this.coordinateInfo
                    .get(((ClusterVertex) vertices[i]).getInfo());
            double x = coordinate[0] * (width - 40) + 20;
            double y = coordinate[1] * (height - 40) + 20;
            //System.err.printf("[%f, %f]", x, y);
            coord.setX(x);
            coord.setY(y);
        }
    }
}
